package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.User;
import com.itheima.service.UserDateService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/userdate")
public class UserDateController {
    @Reference
    private UserDateService userDateService;
    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody User user){
        try{
            userDateService.add(roleIds,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userDateService.findPage(queryPageBean);
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user = userDateService.findById(id);
        if(user != null){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(user);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }

    //根据检查组合id查询对应的所有检查项id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try{
            List<Integer> roleIds =
                    userDateService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(Integer[] roleIds, @RequestBody User user){
        try{
            userDateService.edit(roleIds,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<User> userList = userDateService.findAll();
        if(userList != null && userList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(userList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }
    //删除检查项
    @RequestMapping("/deleteById")

    public Result deleteById(Integer id){
        try{
            userDateService.deleteById(id);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }
}


